/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
// 哈希表
var containsNearbyDuplicate = function (nums, k) {
    const map = new Map()
    for (let i = 0; i < nums.length; i++) {
        if (map.has(nums[i]) && i - map.get(nums[i]) <= k) return true
        map.set(nums[i], i)
    }
    return false
};

// 滑动窗口
var containsNearbyDuplicate = function (nums, k) {
    const set = new Set()
    const length = nums.length
    for (let i = 0; i < length; i++) {
        if (i > k) {
            set.delete(nums[i - k - 1])
        }
        if (set.has(nums[i])) return true

        set.add(nums[i])
    }

    return false
};
const result = containsNearbyDuplicate([1, 2, 3, 1, 2, 3], 2)
console.log("result >>", result);